﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace Event_SG_EWDT
{
    public class GroupControl
    {
        // This class includes the code from Group and MemberGroup

        // When a user is creating a group the system calls creatGroup()
        // When a user is updating the GroupInfo the system calls updateGroup()

        #region  Group

        public static string CreateGroup(string member_ID,string GroupName, string Description, int GroupPrivacy, int groupTypeId, string account)
        {
            try
            {
                Group g = new Group();

                g.GroupName = GroupName;
                g.Description = Description;
                g.GroupPrivacy = GroupPrivacy;
                g.GroupTypeId = groupTypeId.ToString(); 
                Group.listGroup.Add(g);
                g.GroupId = DBMANAGER.CreateGroup(g);

                CreateMemberGroup(member_ID, g.GroupId, "Admin", 1);
                return g.GroupId;
            }
            catch
            {
                return null;
            }
        }
        public static void RetrieveGroup()
        {
           Group.listGroup = DBMANAGER.GetAllGroup();
        }
        public static Group RetrieveGroup(string groupid)
        {
            return DBMANAGER.GetAGroup(groupid);
        }
        public static int GetGroupIndexFromList(string GroupID)   // loop the list to get the Group using GroupID
        {
            for (int i = 0; i < Group.listGroup.Count; i++)
            {
                if (Group.listGroup[i].GroupId == GroupID)
                {
                    return i;
                }
            }
            return -1;
        }
        public static bool UpdateGroup(string GroupID, string GroupName, string Description, int GroupPrivacy, string groupTypeid)
        {
            try
            {
                Group tempG = new Group();
                tempG.GroupId = GroupID;
                tempG.GroupName = GroupName;
                tempG.Description = Description;
                tempG.GroupPrivacy = GroupPrivacy;
                tempG.GroupTypeId = groupTypeid;   // GroupType should be generate from UI level (dropdownlist?)

                return DBMANAGER.UpdateGroup(tempG);

            }
            catch
            {
                return false;
            }

        }
        public static int GetNumberofMembers(string groupid)
        {
            return DBMANAGER.GetNumberOfMember(groupid) ;

        }

        #endregion


        // When a user is creating a member into a group(join a Group), the system calls createMemberGroup()
        // When a user is changing the role of member calls UpdateMemberType()

        #region  MemberGroup

        public static bool CreateMemberGroup(string MemberShip_No, string GroupID, string MemberType, int Status)
        {
            MemberGroup MG = new MemberGroup();
            MG.MembershipNo = MemberShip_No;
            MG.GroupId = GroupID;
            MG.MemberType = MemberType;
            MG.Status = Status;

           return DBMANAGER.AddMemberGroup(MG);
        }
        public static List<MemberGroup> RetrieveMemberGroupByMemberID(string memid)
        {
            
           return DBMANAGER.RetrieveMemberGroupByMemberID(memid);

        }
        public static List<MemberGroup> RetrieveMemberGroupByGroupID(string groupID)
        {
            return DBMANAGER.RetrieveMemberGroupByGroupid(groupID);
          //  ConvertMemberGroup();
        }
        //public static void ConvertMemberGroup()
        //{
        //    MemberGroup.groupPendingList.Clear();
        //    MemberGroup.groupInviteList.Clear();
        //    for (int i = 0; i < MemberGroup.listMemberGroup.Count; i++)
        //    {
        //        if (MemberGroup.listMemberGroup[i].Status == 0)
        //        {
        //            MemberGroup.groupInviteList.Add(MemberGroup.listMemberGroup[i]); 
        //        }
        //        if (MemberGroup.listMemberGroup[i].Status == 3)
        //        {
        //            MemberGroup.groupPendingList .Add(MemberGroup.listMemberGroup[i]);
        //        }
        //    }
        //}
        public static bool UpdateMemberType(string MemberShip_No, string GroupID, string MemberType)
        {
            //int i = GetIndexOfMemberGroup(MemberShip_No, GroupID);

            MemberGroup mg = new MemberGroup();
            mg.MembershipNo = MemberShip_No;
            mg.GroupId = GroupID;
            mg.MemberType = MemberType;

            return DBMANAGER.UpdateMemberGroup_Membertype(mg); // untest code
        }
        
        public static bool UpdateMemberGroupStatus(string MemberShip_No, string GroupID, int Status)
        {
            try
            {
                MemberGroup mg = new MemberGroup();
                mg.MembershipNo = MemberShip_No;
                mg.GroupId = GroupID;
                mg.Status = Status;

                return DBMANAGER.UpdateMemberGroup_status(mg);
            }

            catch
            {

                return false;
            }
        }
        
        //public static int GetIndexOfMemberGroup(string MemberShip_No, string GroupID)
        //{
        //    for (int i = 0; i < MemberGroup.listMemberGroup.Count; i++)
        //    {
        //        if (MemberGroup.listMemberGroup[i].MembershipNo == MemberShip_No && MemberGroup.listMemberGroup[i].GroupId == GroupID)
        //        {

        //            return i;
        //        }
        //    }
        //    return -1;
        //}
        public static int FindAdmin(string groupid)
        {
           return DBMANAGER.getAdminForGroup(groupid);
        }
        #endregion


        // When a user wants to join group call JoinGroup
        // When a user wants to invite someone call InviteToGruop
        // When a user wants to leave group call LeaveGroup


        public static void InviteToGroup(string membership_No,string account_email, string groupNo)
        {
           if (MemberControls.IsAccountExist(account_email))
           {
                MemberGroup MG = new MemberGroup();
                MG.MembershipNo = membership_No;
                MG.GroupId = groupNo;
                MG.MemberType = "Member";
                MG.Status = 0;// invite waiting

                CreateMemberGroup(MG.MembershipNo, MG.GroupId, MG.MemberType, MG.Status);

            }
            else
            {
                // If user doesn't exist
                // Send an invitation email to the person?

            }

        }

        #region JoinGroup
        public static int JoinGroup(string GroupID, String MemberID)
        {
            try
            {
                //int i = getGroupIndexFromList(GroupID);
                Group G = DBMANAGER.GetAGroup(GroupID);
               // Group G = Group.listGroup[i];
                return  CheckGroupPrivacy(GroupID, MemberID, G.GroupPrivacy);
                
            }
            catch
            {
                return -1;
            }
        }
        public static int CheckGroupPrivacy(string GroupID, string MemberID, int Privacy)
        {
            if (Privacy == 0)   // free to join 
            {
                CreateMemberGroup(MemberID, GroupID, "Member", 1);  // status 1 means joined
                return 1;
            }
            if (Privacy == 1)   // private Group must wait for approval
            {
                MemberGroup mg = new MemberGroup();
                mg.MembershipNo = MemberID;
                mg.GroupId = GroupID;
                mg.MemberType = "Member";
                mg.Status = 3; // status 3 means waiting for approval                   
                //MemberGroup.listMemberGroup.Add(mg);         
                //MemberGroup.groupPendingList.Add(mg);

                CreateMemberGroup(MemberID, GroupID, "Member", 3);
                

                return 99;
            }
            if (Privacy == 2)
            {
                // You are not allowed to join by yourself.
                return -1;
            }
            return -1;
        }

        public static List<MemberGroup> isJoin(string groupID, string memberID)
        {

         return DBMANAGER.RetrieveAMemberGroupinList(groupID, memberID);

        }
        public static int isPending(List<MemberGroup> membergroup)
        {// Run IsJoin before this.
            //0	inviting-waiting
            //1	inviting-accept
            //2	inviting-reject
            //3	inviting-pending

            List<MemberGroup> a = membergroup;
            if (a.Count == 0)
            {
                return -1;
            }

            return a[0].Status;
        }
        public static bool IsAdmin(List<MemberGroup> membergroup)
        {// Run IsJoin before this.
            List<MemberGroup> a = membergroup;

                if (a[0].MemberType.ToLowerInvariant() == "admin")
                {
                    return true;
                }
            
            return false;
        }
        public static bool IsAdmin(string memberID, string groupID)
        {
            List<MemberGroup> a = DBMANAGER.RetrieveAMemberGroupinList(groupID, memberID);
            if (a[0].GroupId == groupID)
            {
                if (a[0].MemberType.ToLowerInvariant() == "admin")
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
        public static void LeaveGroup(String GroupID, String MemberID)
        {
            MemberGroup mg = new MemberGroup();
            mg.MembershipNo = MemberID;
            mg.GroupId = GroupID;
            DBMANAGER.DeleteMemberGroup(mg);
            //UpdateMemberStatus(MemberID, GroupID, 2); // status 2 means reject
        }

    }
}